package com.boss.salary.controller;

import com.boss.api.dto.ApiResultDTO;
import com.boss.common.base.BasicinfoBaseController;
import com.boss.salary.entity.OperrateLogs;
import com.boss.salary.entity.SalaryWarnMonth;
import com.boss.salary.service.IOperrateLogsService;
import com.boss.salary.service.ISalaryWarnMonthService;
import com.boss.sys.ca.user.dto.UserDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.core.MediaType;
import java.util.Map;

@RestController
@RequestMapping(value = "/salaryWarnMonthController", produces = MediaType.APPLICATION_JSON)
public class SalaryWarnMonthController extends BasicinfoBaseController {

    /**
     * 引入日志框架常量
     */
    private final static Logger logger = LoggerFactory.getLogger(SalaryWarnMonthController.class);

    @Autowired
    private ISalaryWarnMonthService iSalaryWarnMonthService;
    @Autowired
    private IOperrateLogsService iOperrateLogsService;

    /**
     * 修改
     * @param salaryWarnMonth
     * @return
     */
    @PostMapping("/updateSalaryWarnMonth")
    public ApiResultDTO updateSalaryWarnMonth(@RequestHeader("X-Token") String tokenid,
                                              @RequestBody SalaryWarnMonth salaryWarnMonth) {
        try {
            UserDTO userDTO = getUser(tokenid);

            //添加操作日志
            OperrateLogs operrateLogs= new OperrateLogs();
            operrateLogs.setMofDivCode(userDTO.getProvince());
            operrateLogs.setTokenId(tokenid);
            operrateLogs.setUserAgencyId(userDTO.getAgency());
            operrateLogs.setUeserName(userDTO.getName());
            operrateLogs.setOperateName("工资管理配置--预警设置--修改");
            iOperrateLogsService.saveOperrateLogs(operrateLogs);

            iSalaryWarnMonthService.updateSalaryWarnMonth(salaryWarnMonth);
            return ApiResultDTO.success(null, "修改成功");
        } catch (Exception e) {
            logger.error("修改 财务预警异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, e.getMessage());
        }
    }

    /**
     * 查询
     * @return
     */
    @GetMapping("/getSalaryWarnMonth")
    public ApiResultDTO getSalaryWarnMonth(@RequestHeader("X-Token") String tokenid){
        try {
            String province = getUser(tokenid).getProvince();
            SalaryWarnMonth salaryWarnMonth = iSalaryWarnMonthService.getSalaryWarnMonth(province);
            if(null==salaryWarnMonth){
                return ApiResultDTO.error(ERROR_RSCODE, "查询失败");
            }else{
                return ApiResultDTO.success(salaryWarnMonth, "查询成功");
            }

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询失败");
        }

    }
    /**
     * 检测指标月份是否满足预警月份
     * @param salaryWarnMonth
     * @return
     */
    @PostMapping("/checkWarnMonthLoading")
    public ApiResultDTO checkWarnMonthLoading(@RequestHeader("X-Token") String tokenid,
                                              @RequestBody SalaryWarnMonth salaryWarnMonth) {
        try {
            UserDTO userDTO = getUser(tokenid);
            Map<String,Object> map =iSalaryWarnMonthService.checkWarnMonthLoading(salaryWarnMonth,userDTO);
            return ApiResultDTO.success(map);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("检测指标月份是否满足预警月份失败", e.getMessage());
            return ApiResultDTO.error(ERROR_RSCODE, e.getMessage());
        }
    }
}
